<template>
  <div class="erwei">
    <div class="img" v-loading="loading">
      <img :src="qrImgUrl" alt="" class="erweima" />
      <i
        class="mask"
        v-show="showQrMask"
        :style="maskBackground"
        @click="updateQr"
      ></i>
    </div>

    <p v-show="!showQrMask">请使用手机微信扫码登入</p>
    <p v-show="showQrMask">二维码失效，请点击刷新二维码</p>

    <svg class="icon diannao" aria-hidden="true" @click="typeCommon">
      <use xlink:href="#icon-diannao"></use>
    </svg>
  </div>
</template>
<script>
import scoketIo from "socket.io-client";
import qrcode from "qrcode";
import * as api from "@/api/users";
import invalidImg from "@/assets/images/invalidQr.png";
import scanSuccessImg from "@/assets/images/scancode.png";
import storage from "@/utils/storage";

export default {
  props: {
    storageUserInfo: {
      required: true,
    },
  },
  data() {
    return {
      qrImgUrl: "",
      showQrMask: false,
      loading:true,
      maskBackground: { backgroundImage: `url(${scanSuccessImg})` },
      // maskBgInvalid:{background:url(invalidImg)},
    };
  },

  created() {
    // console.log(this.storageUserInfo);
    let io = scoketIo("wss://chst.vip");
    io.on("connectSuccess", ({ sid }) => {
      storage.set("qf-sid", sid);
      // console.log(sid);
      this.getQrUrl(sid);
    });
    io.on("invalidCode", (data) => {
      // console.log(data);
      this.showQrMask = true;
      this.maskBackground.backgroundImage = `url(${invalidImg})`;
    });

    io.on("scancodeSuccess", async ({ state, wechatCode, msg }) => {
      this.showQrMask = true;
      this.maskBackground.backgroundImage = `url(${scanSuccessImg})`;
      if (state) {
        let res = await api.wechatLoginApi(wechatCode);
        // console.log(res);
        this.storageUserInfo(res.data);

        this.$router.push("/");
      } else {
        console.log(msg);
      }
    });
  },
  methods: {
    async getQrUrl(sid) {
       this.loading=true
      let res = await api.getQrCodeApi(sid);
      // console.log(res);
      let { scanCodeUrl: qrCodeUrl } = res.data;
      // console.log(qrCodeUrl)
      // this.qrImgUrl=qrCodeUrl
      qrcode.toDataURL(qrCodeUrl, (err, img) => {
        if (err) {
          throw err;
        } else {
          // console.log(img)
          this.qrImgUrl = img;
          this.showQrMask = false;
          this.loading=false
        }
      });
    },
    updateQr() {
      let sid = storage.get("qf-sid");
      this.getQrUrl(sid);
    },
    typeCommon() {
      this.$emit("typeCommon");
    },
  },
};
</script>

<style lang="less" scoped>
.erwei {
  text-align: center;
  color: #fff;
}

.img {
  /* background-color: red; */
  text-align: center;
  color: #fff;
  width: 200px;
  height: 200px;
  margin-left: 150px;
  position: relative;
}
.mask {
  width: 200px;
  height: 200px;
  background-color: rgba(0, 0, 0, 0.3);
  display: inline-block;
  position: absolute;
  left: 0;
  background-size: 120px;
  background-repeat: no-repeat;
  background-position: center;
  cursor: pointer;
}

.erweima {
  width: 200px;
  height: 200px;
  // background-image: url(./../../assets/images/scancode.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  /* background-color: aqua; */
  display: inline-block;
}
.erweima-false {
  width: 200px;
  height: 200px;
  // background-image: url(./../../assets/images/invalidQr.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  /* background-color: aqua; */
  display: inline-block;
}
// .icon {
//   width: 1em;
//   height: 1em;
//   vertical-align: -0.15em;
//   fill: currentColor;
//   overflow: hidden;
// }
.diannao {
  font-size: 80px;
  color: #ccc;
  position: absolute;
  right: -4px;
  top: -10px;
  opacity: 0.7;
  cursor: pointer;
}
</style>